<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="检测名称" prop="checkName">
        <el-input
          v-model="queryParams.checkName"
          placeholder="请输入检测名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检测机构" prop="checkOrgan">
        <el-input
          v-model="queryParams.checkOrgan"
          placeholder="请输入检测机构"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品" prop="checkProduct">
        <el-input
          v-model="queryProductName"
          placeholder="请输入产品"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        >
        <el-button slot="append" icon="el-icon-search" @click="selectProduct(0)" :disabled="false"></el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="检测日期" prop="checkProvideTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.checkProvideTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择检测日期">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="图片" prop="checkPic">
        <el-input
          v-model="queryParams.checkPic"
          placeholder="请输入图片"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="证明人" prop="checkPerson">
        <el-input
          v-model="queryParams.checkPerson"
          placeholder="请输入证明人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="证明人联系方式" prop="checkPersonPhone">
        <el-input
          v-model="queryParams.checkPersonPhone"
          placeholder="请输入证明人联系方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['beyond:check:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['beyond:check:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['beyond:check:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['beyond:check:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="" align="center" prop="checkId" /> -->
      <el-table-column label="检测名称" align="center" prop="checkName" />
      <el-table-column label="检测机构" align="center" prop="checkOrgan" />
      <el-table-column label="产品" align="center" prop="productName" />
      <el-table-column label="检测日期" align="center" prop="checkProvideTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkProvideTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="图片" align="center" prop="checkPic" /> -->
      <el-table-column label="证明人" align="center" prop="checkPerson" />
      <el-table-column label="证明人联系方式" align="center" prop="checkPersonPhone" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['beyond:check:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['beyond:check:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品质量检测信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="检测名称" prop="checkName">
          <el-input v-model="form.checkName" placeholder="请输入检测名称" />
        </el-form-item>
        <el-form-item label="检测机构" prop="checkOrgan">
          <el-input v-model="form.checkOrgan" placeholder="请输入检测机构" />
        </el-form-item>
        <el-form-item label="产品" prop="checkProduct">
          <el-input v-model="productName" placeholder="请选择产品" >
            <el-button slot="append" icon="el-icon-search" @click="selectProduct(1)" :disabled="false"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="检测日期" prop="checkProvideTime">
          <el-date-picker clearable size="small"
            v-model="form.checkProvideTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择检测日期">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="图片" prop="checkPic">
          <el-input v-model="form.checkPic" placeholder="请输入图片" />
        </el-form-item> -->
        <el-form-item label="证明人" prop="checkPerson">
          <el-input v-model="form.checkPerson" placeholder="请输入证明人" />
        </el-form-item>
        <el-form-item label="证明人联系方式" prop="checkPersonPhone">
          <el-input v-model="form.checkPersonPhone" placeholder="请输入证明人联系方式" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form :inline="true">
            <el-form-item label="检测图片" prop="checkPic" style="width:100%;margin:0;">
              <ImageUpload2 @input="imgInput" :limit="1" :uploadUrl="url" :value="form.checkPic"></ImageUpload2>
            </el-form-item>
        </el-form>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="产品选择" :visible.sync="productOpen" width="900px">
      <div>
        <ProductSelect @productSelect="productSelect"/>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="productSelectSubmitForm">确 定</el-button>
        <el-button @click="productSelectCancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listCheck, getCheck, delCheck, addCheck, updateCheck, exportCheck } from "@/api/trace/check";
import ProductSelect from '@/components/ProductSelect';
import ImageUpload2 from "@/components/ImageUpload2"

export default {
  components: {
    ProductSelect,
    ImageUpload2
  },
  name: "Check",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 查询产品名称
      queryProductName: '',
      // 产品名称
      productName: '',
      // 选择产品值
      selectProductValue: null,
      //是否显示产品选择对话框
      productOpen: false,
      //判断选择产品用于查询还是新增
      selectProductType: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品质量检测信息表格数据
      checkList: [],
      // 弹出层标题
      title: "",
      // 上传url
      url: "trace/check/picture/upload",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checkName: null,
        checkOrgan: null,
        checkProduct: null,
        checkProvideTime: null,
        checkPic: null,
        checkPerson: null,
        checkPersonPhone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品质量检测信息列表 */
    getList() {
      this.loading = true;
      listCheck(this.queryParams).then(response => {
        this.checkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        checkId: null,
        checkName: null,
        checkOrgan: null,
        checkProduct: null,
        checkProvideTime: null,
        checkPic: null,
        checkPerson: null,
        checkPersonPhone: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
      this.queryProductName = '';
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
      this.queryProductName = '';
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.checkId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.productName = ''
      this.open = true;
      this.title = "添加产品质量检测信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const checkId = row.checkId || this.ids
      getCheck(checkId).then(response => {
        this.form = response.data;
        this.productName = response.data.productName
        this.open = true;
        this.title = "修改产品质量检测信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.checkId != null) {
            updateCheck(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheck(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const checkIds = row.checkId || this.ids;
      this.$confirm('是否确认删除产品质量检测信息编号为"' + checkIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCheck(checkIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有产品质量检测信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCheck(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    //选择商品
    selectProduct(type){
      if(type == 0){
        this.selectProductType = true;
        this.queryProductName = ''
        this.productOpen = true;
      }else{
        this.selectProductType = false;
        this.productName = ''
        this.productOpen = true;
      }
    },
    // 产品选择点击取消
    productSelectCancel(){
      this.productOpen = false;
    },
    //选中产品传递
    productSelect(data){
      this.selectProductValue = data;
    },
    //产品选择点击确定
    productSelectSubmitForm(){
      if(this.selectProductType){
        this.queryParams.checkProduct = this.selectProductValue.productId
        this.queryProductName = this.selectProductValue.productName
        this.productOpen = false;
      }else{
        this.form.checkProduct = this.selectProductValue.productId
        this.productName = this.selectProductValue.productName
        this.productOpen = false;
      }
      
    },
    imgInput(value){
      this.form.checkPic = value
    },
    
  }
};
</script>
